﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Elderos.Highlight.Search;
using Elderos.Ontology;

namespace Elderos.Highlight.Rules
{
    [RankingRule("InSignature", RuleType.Text)]
    public class RuleInSignature : RankingRule
    {
        public RuleInSignature(IOntologyData ontology) : base(ontology)
        {
        }

#pragma warning disable 1998
        protected async override Task<Dictionary<TextEntity, IRuleResult>> InnerRank(HighlightResult highlightResult)
#pragma warning restore 1998
        {
            var result = new Dictionary<TextEntity, IRuleResult>();

            int textLength = highlightResult.Tokens.Length;
            foreach (var position in highlightResult.Positions.SelectMany(x => x.SubpositionsFlattened))
            {
                int tokenPosition = position.TokenInterval.Last;
                foreach (var textEntity in position.Entities)
                {
                    result.Add(textEntity, new BinomialResult(Name, textLength - tokenPosition < 10));
                }
            }

            return result;
        }

        public override IRuleResult EmptyResult
        {
            get { return new BinomialResult(Name, false);}
        }
    }
}
